<?php
  /* Simple utility functions */
function get_address_hash($address1="z", $address2="z", $address3="z") {
  return md5($address1 . $address2 . $address3);
}

function coord_to_address($lat, $lng) {
  define('API_KEY', "459c244ff059e0d3a7fc6b0d1e7b3127edec7423");
  return file_get_contents('http://apis.daum.net/maps/coord2addr?apikey=' . API_KEY .
			   '&output=xml&format=simple&inputCoordSystem=WGS84&latitude=' . $lat . '&longitude=' . $lng);
}

function get_current_time()
{
  return date("Y-m-d H:i:s");
}

function add_date($givendate,$day=0,$mth=0,$yr=0) {
  $cd = strtotime($givendate);
  $newdate = date('Y-m-d h:i:s', mktime(date('h',$cd),
					date('i',$cd), date('s',$cd), date('m',$cd)+$mth,
					date('d',$cd)+$day, date('Y',$cd)+$yr));
  return $newdate;
}

function shorten_title($title, $length=19) {
  if (mb_strlen($title, 'utf-8') > $length) {
    return mb_substr($title, 0, $length, 'utf-8') . '...';
  } else {
    return $title;
  }
}


function print_r2_callback($buffer)
{
  // replace all the apples with oranges
  $str = str_replace(" ", "&nbsp;", $buffer);
  $str = str_replace("\n", "<br />", $str);
  return $str;
}


function print_r2($str)
{
  ob_start("print_r2_callback");
  print_r($str);
  ob_end_flush();
}


function redirect($url,$permanent = false)
{
  if($permanent)
    {
      header('HTTP/1.1 301 Moved Permanently');
    }
  header('Location: '.$url);
  exit();
}

function json_esc($text)
{
  $uesc = array("\"", "'");
  $esc = array("&quot;", "&#39;");

  return str_replace($uesc, $esc, $text);
}

?>
